import { Inject, Post, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { MallGoodsEntity } from '../../entity/goods';
import { MallGoodsService } from '../../service/goods';
import { MallCategoryEntity } from '../../entity/category';

/**
 * 商品信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: MallGoodsEntity,
  service: MallGoodsService,
  pageQueryOp: {
    keyWordLikeFields: ['a.title'],
    fieldEq: ['a.categoryId', 'a.status'],
    where: async ctx => {
      const { priceStart, priceEnd } = ctx.request.body;
      const where = [];
      if (priceStart) {
        where.push(['a.price >= :priceStart', { priceStart }]);
      }
      if (priceEnd) {
        where.push(['a.price <= :priceEnd', { priceEnd }]);
      }
      return where;
    },
    join: [
      {
        entity: MallCategoryEntity,
        alias: 'b',
        condition: 'a.categoryId = b.id',
        type: 'leftJoin',
      },
    ],
    select: ['a.*', 'b.name as categoryName'],
  },
})
export class AdminMallGoodsController extends BaseController {
  @Inject()
  mallGoodsService: MallGoodsService;
}
